<template>
	<view class="content-box">
		<songList :songItems="recommendSongList"></songList>
		<view class="play-bar">
			<playBar :musicItems="playSongList"></playBar>
		</view>
		<!-- 加载页面动画 -->
		<Loading :isLoading="isLoading"></Loading>
	</view>
</template>

<script>
	import songList from '../../components/songList/songList.vue'
	import playBar from '../../components/BottomBar/index.vue'
	import Loading from '../../components/loading/loading.vue'
	const backgroundAudio = uni.getBackgroundAudioManager();
	export default {
		components: {
			songList,
			playBar,
			Loading
		},
		data() {
			return {
				// 是否加载页面
				isLoading:false,
				// 查询条件
				queryInfo: {
					id: 0,
					_p: 0
				},
				// 歌单列表
				recommendSongList: [],
				// 音乐播放信息
				playSongList: {
					name: '',
					artist: '',
					src: '',
					pic: '',
					isplay: false,
					playTime: 0
				},
				// 计时器
				countInterval: 0,
			};
		},
		onLoad(e) {



			if (uni.getStorageSync("musicItem") != '') {
				this.playSongList = uni.getStorageSync("musicItem")

			}

			if (e) {
				this.queryInfo.id = e.id
				this.queryInfo._p = e._p
			}

			this.getRecommendSongList()
			
			this.isLoading = true
			


		},
		onShow() {
			if (uni.getStorageSync("musicItem") != '') {
				this.playSongList = uni.getStorageSync("musicItem")
			}
			uni.setStorageSync("currentPage", "recommendSong")
			
		},
		methods: {
			getRecommendSongList() {
				uni.request({
					url: 'http://music.jsososo.com/apiMix/playlist',
					data: {
						id: this.queryInfo.id,
						_p: this.queryInfo._p
					},
					success: res => {

						res.data.data.list.map((item, index) => {

							if (item.id == this.playSongList.id) res.data.data.list[index].isplay =
								this.playSongList.isplay

						})

						this.recommendSongList = res.data.data.list


					},
					fail: (err) => {
						console.log('获取歌单内容失败')
					}
				})
			},
			// 播放音乐
			playMusic(songItem) {

				uni.removeStorageSync("musicItem")
				uni.request({
					url: 'http://music.jsososo.com/api/song/url',
					data: {
						id: songItem.id,

					},
					success: res => {

						backgroundAudio.title =songItem.name
						backgroundAudio.singer =songItem.ar[0].name
						backgroundAudio.coverImgUrl =songItem.al.picUrl
						backgroundAudio.src = res.data.data[0].url
						this.playSongList.src = res.data.data[0].url
						// if (innerAudioContext.paused) {
						// 	innerAudioContext.play()
						// 	innerAudioContext.onPlay(() => {
						// 		console.log('开始播放');
						// 	});
						// 	innerAudioContext.onError((res) => {
						// 		console.log(res.errMsg);
						// 		console.log(res.errCode);
						// 	});

						// } else {
						// 	innerAudioContext.stop()
						// 	innerAudioContext.play()
						// 	innerAudioContext.onPlay(() => {
						// 		console.log('开始播放');
						// 	});
						// 	innerAudioContext.onError((res) => {
						// 		console.log(res.errMsg);
						// 		console.log(res.errCode);
						// 	});

						// }

					}
				})
				this.playSongList.id = songItem.id
				this.playSongList.pic = songItem.al.picUrl
				this.playSongList.artist = songItem.ar[0].name
				this.playSongList.name = songItem.name

				this.playSongList.isplay = true

				this.recommendSongList.map((item, index) => {
					if (item.id == songItem.id) this.recommendSongList[index].isplay = true
					else {
						this.recommendSongList[index].isplay = false
					}
				})


				this.playSongList.playTime = songItem.duration


				uni.setStorageSync('musicItem', this.playSongList)
			},
			// 音乐计时器
			musicInterVal() {
				this.countInterval = setInterval(() => {

					if (uni.getStorageSync("currentPage") == "recommendSong" && (uni.getStorageSync(
							"currentPlaySongPage") == "searchSong" || uni.getStorageSync(
							"currentPlaySongPage") == "indexSong")) {
						if (uni.getStorageSync("musicOnPlay") == '1') {
							this.recommendSongList.map((item, index) => {
								if (item.id == this.playSongList.id) this.recommendSongList[index].isplay =
									true
								
							})
							this.playSongList.isplay = true
						} else if (uni.getStorageSync("musicOnPlay") == '0') {
							this.playSongList.isplay = false
							this.recommendSongList.map((item, index) => {
								if (item.id == this.playSongList.id) this.recommendSongList[index].isplay =
									false
								
							})
						}


					} else if (backgroundAudio.duration != false && backgroundAudio.currentTime != false) {
						if ((backgroundAudio.duration - backgroundAudio.currentTime) <= 0.5 && uni
							.getStorageSync("currentPlaySongPage") == "recommendSong") {
							backgroundAudio.stop()
							this.playSongList.isplay = false

							this.recommendSongList.map((item, index) => {
								if (item.id == this.playSongList.id) this.recommendSongList[index].isplay =
									false

							})

							uni.setStorageSync('musicOnPlay', '0')

							uni.setStorageSync('musicItem', this.playSongList)
						}

					}




				}, 1000)
			}
		},
		onReady() {
			setTimeout(()=>{
				this.isLoading = false
			},2000)
			// 监听音乐是否结束
			this.musicInterVal()
			// 被其他页面调用
			uni.$on("controlRecommendMusic", (msg, data) => {
				if (msg == "playMusic") {
					this.playMusic(data)
					backgroundAudio.pause()
					backgroundAudio.play()
					this.recommendSongList.map((item, index) => {
						if (item.id == this.playSongList.id) this.recommendSongList[index].isplay = true
					})
					this.playSongList.isplay = true
					uni.setStorageSync('musicOnPlay', '1')
					uni.setStorageSync('musicItem', this.playSongList)
				} else if (msg == "pauseMusic") {
					backgroundAudio.pause()
					this.recommendSongList.map((item, index) => {
						if (item.id == this.playSongList.id) this.recommendSongList[index].isplay = false
					})
					this.playSongList.isplay = false
					uni.setStorageSync('musicOnPlay', '0')

					uni.setStorageSync('musicItem', this.playSongList)
				} else if (msg == "stopMusic") {
					backgroundAudio.stop()
				} else if (msg == "refreshMusicStatus") {
					this.playSongList = uni.getStorageSync("musicItem")
				} else if (msg == "refreshCurrentTime") {

					uni.setStorageSync('duration', backgroundAudio.duration)
					uni.setStorageSync("currentTime", backgroundAudio.currentTime)
				}
			})
		},

	}
</script>

<style lang="scss">
	.content-box {
		.play-bar {}
	}
</style>
